<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('文件归档') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('申请标题')" prop="actTitle">
        <el-input
          v-model="queryParams.actTitle"
          :placeholder="$t('申请标题')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('用印一级')" prop="sealTypeOne">
        <el-select v-model="queryParams.sealTypeOne" :placeholder="$t('用印一级')" clearable>
          <el-option
            v-for="dict in sealTypeOneOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('用印二级')" prop="sealTypeTwo">
        <el-select v-model="queryParams.sealTypeTwo" :placeholder="$t('用印二级')" clearable>
          <el-option
            v-for="dict in sealTypeTwoOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('存放地')" prop="storagePlace">
        <el-input
          v-model="queryParams.storagePlace"
          :placeholder="$t('存放地')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <!--
      <el-form-item :label="$t('创建时间')" prop="applyTime">
        <el-date-picker
          v-model="queryParams.applyTime"
          clearable
          style="width: 200px"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('创建时间')"
        />
      </el-form-item>
      <el-form-item :label="$t('预计归档时间')" prop="estimatedArchivingTime">
        <el-date-picker
          v-model="queryParams.estimatedArchivingTime"
          clearable
          style="width: 200px"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('预计归档时间')"
        />
      </el-form-item>
      -->
      <el-form-item :label="$t('是否完成归档')" prop="archiveCompleted">
        <el-select v-model="queryParams.archiveCompleted" :placeholder="$t('请选择')" clearable @keyup.enter.native="queryHandle">
          <el-option value="是">{{ $t('是') }}</el-option>
          <el-option value="否">{{ $t('否') }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['oa:filing:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
      <!--
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['oa:filing:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t('导出') }}
        </el-button>
      </el-col>
      -->
    </el-row>

    <el-table v-loading="loading" :data="oaFilingList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请标题')" align="center" prop="actTitle" min-width="200">
        <template slot-scope="scope">
          <el-link type="primary" :underline="false" @click="updateHandle(scope.row)">{{ scope.row.actTitle }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('关联用印')" align="center" prop="sealId" min-width="150" :formatter="sealTypeIdFormat" />
      <el-table-column :label="$t('用印类型一级')" align="center" prop="sealTypeOne" :formatter="sealTypeOneFormat" min-width="130" />
      <el-table-column :label="$t('用印类型二级')" align="center" prop="sealTypeTwo" :formatter="sealTypeTwoFormat" min-width="130" />
      <el-table-column :label="$t('归档人')" align="center" prop="archivingPersonnelName" min-width="130" />
      <el-table-column :label="$t('保管人')" align="center" prop="custodianName" min-width="130" />
      <el-table-column :label="$t('存放地')" align="center" prop="storagePlace" min-width="130" />
      <el-table-column :label="$t('创建时间')" align="center" prop="applyTime" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('预计归档时间')" align="center" prop="estimatedArchivingTime" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.estimatedArchivingTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('是否完成归档')" align="center" prop="archiveCompleted" min-width="130" />
      <el-table-column :label="$t('审批状态')" fixed="right" align="center" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <!-- 启动流程，终止流程，修改，删除 四个按钮 -->
          <ListBtn :bus-data="scope.row" has-permi="oa:filing" :other-btn="!(scope.row.archiveCompleted === '否' && scope.row.actStatus === 'W')" />
          <el-button
            v-if="scope.row.archiveCompleted === '否' && scope.row.actStatus === 'W'"
            size="mini"
            type="text"
            class="el-icon-circle-check"
            @click="archive(scope.row)"
          >{{ $t('归档') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文件归档对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <oaFilingForm ref="oaFilingForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listOaFiling, delOaFiling, exportOaFiling } from '@/api/oa/filing/oaFiling'
import oaFilingForm from './oaFilingForm.vue'
import { listOaSeal } from '@/api/oa/seal/oaSeal'

export default {
  components: {
    oaFilingForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 文件归档表格数据
      oaFilingList: [],
      // 弹出层标题
      title: '文件归档',
      // 是否显示弹出层
      formOpen: false,
      // 用印类型一级字典
      sealTypeOneOptions: [],
      // 用印类型二级字典
      sealTypeTwoOptions: [],
      // 查询参数
      queryParams: {
        sealId: undefined,
        archivingPersonnel: undefined,
        sealTypeOne: undefined,
        sealTypeTwo: undefined,
        storagePlace: undefined,
        custodian: undefined,
        applyTime: undefined,
        estimatedArchivingTime: undefined,
        archiveCompleted: undefined,
        actId: undefined,
        actStatus: undefined,
        actTitle: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      sealListOption: []
    }
  },
  created() {
    this.getList()
    listOaSeal({ actStatus: 'W' }).then(response => {
      this.sealListOption = []
      if (response.rows !== null) {
        response.rows.forEach(item => {
          this.sealListOption.push({
            dictValue: item.id,
            dictLabel: item.actTitle
          })
        })
      }
    })
    this.getDicts('seal_usine_type').then(response => {
      this.sealTypeOneOptions = response.data
    })
    this.getDicts('seal_usine_type_two_all').then(response => {
      this.sealTypeTwoOptions = response.data
    })
  },
  methods: {
    /** 查询文件归档列表 */
    getList() {
      this.loading = true
      listOaFiling(this.queryParams).then(response => {
        this.oaFilingList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 用印类型一级字典翻译
    sealTypeOneFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.sealTypeOneOptions, row.sealTypeOne)
    },
    // 用印类型二级字典翻译
    sealTypeTwoFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.sealTypeTwoOptions, row.sealTypeTwo)
    },
    sealTypeIdFormat(row) {
      return this.selectDictLabel(this.sealListOption, row.sealId)
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        sealId: undefined,
        archivingPersonnel: undefined,
        sealTypeOne: undefined,
        sealTypeTwo: undefined,
        storagePlace: undefined,
        custodian: undefined,
        applyTime: undefined,
        estimatedArchivingTime: undefined,
        archiveCompleted: undefined,
        remark: undefined,
        actId: undefined,
        actStatus: '0',
        actTitle: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.oaFilingForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.oaFilingForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return delOaFiling(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return exportOaFiling(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(() => {
      })
    },
    // 归档操作
    archive(row) {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.oaFilingForm.archiveStatus = true
        this.$refs.oaFilingForm.initData(row)
      })
    }
  }
}
</script>
